#include <cstdio>
#include <cstring>
#include <limits.h>
//#include <time.h>
using namespace std;

const int maxcount = 50000;

struct Link
{
  int data;
  Link* next;
  Link* prev;
};

Link linkpool[maxcount];

int main()
{
//	time_t t0 = time(0);
  while( true )
  {
    int n = 0;
    scanf( "%d", &n );
    if( n == 0 ) break;
    if( n == 1 )
    {
      scanf( "%d", &n );
      printf( "0\n" );
      continue;
    }

    memset( linkpool, -1, sizeof(linkpool) );

    for( int i = 0; i < n; i++ )
    {
      Link* p = &linkpool[i];
      scanf( "%d", &p->data );
      p->prev = p - 1;
      p->next = p + 1;
    }
    linkpool[0].prev = NULL;
    linkpool[n - 1].next = NULL;

    Link* head = &linkpool[0];
    Link* start = head;
    int sum = 0;
    for( int i = 0; i < n - 1; i++ )
    {
      Link* p = start;
      int ps = INT_MAX;

      while( p->next != NULL && p->data + p->next->data < ps )
      {
        ps = p->data + p->next->data;
        p = p->next;
      }

      sum += ps;

      Link* tail = p->prev->prev;
      if( tail == NULL )
      { 
        p->data = ps;
        head = head->next;
        head->prev = NULL;
        start = head;
      }
      else
      {
        p->prev->next = p->next;
        if( p->next ) p->next->prev = p->prev;
        p = p->prev;
        start = p;

        while( p->prev != NULL && p->prev->data < ps )
        {
          p->data = p->prev->data;
          p = p->prev;
        }
        p->data = ps;
        start = start->prev;
        if( start->prev != NULL ) start = start->prev;
      }
    }

    printf( "%d\n", sum );
  }
//	printf( "time:%ld\n", time(0) - t0 );  
  return 0;
}

